package com.syj.collect.Present;

import android.util.Log;

import com.google.gson.Gson;
import com.syj.collect.base.BasePresent;
import com.syj.collect.entry.MyCollectBean;
import com.syj.collect.entry.UserInfoBean;
import com.syj.collect.http.BaseObserver;
import com.syj.collect.http.BaseRequest;
import com.syj.collect.utils.SPUtil;
import com.syj.collect.view.CollectActivityView;
import com.syj.collect.view.HomeView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class CollectPresent extends BasePresent<CollectActivityView> {
    public void getCollectList() {
        HashMap hashMap = new HashMap();
        hashMap.put("isCollect", "1");//可选
        String value = SPUtil.getPrefString("userInfo", "");
        Gson gson = new Gson();
        UserInfoBean.UserInfo gBean = gson.fromJson(value, UserInfoBean.UserInfo.class);
        hashMap.put("userId", gBean.getUserId());//不写为所有用户
        hashMap.put("audiStatus", "");//审核状态  0 未审核 2 审核通过 3审核未通过
        String str = gson.toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
        // Log.e("1", str);

        BaseRequest.getInstance().getApiServise().getUserCollectList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
//                        Log.e("tag结果 :", value);
                        Gson gson = new Gson();
                        MyCollectBean gBean = gson.fromJson(value, MyCollectBean.class);
                            if (gBean.getCode() == 0) {
                                view.initSuccess(gBean.getData().getList());
                            } else {
                                view.initFail(gBean.getMsg());
                            }



                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });

    }


    public void addCollect(int dld, boolean isCollect) {
        BaseRequest.getInstance().getApiServise().addCollect(dld,isCollect?1:0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
//                        Log.e("tag结果 :", value);
                        Gson gson = new Gson();
                        MyCollectBean gBean = gson.fromJson(value, MyCollectBean.class);
                        if (gBean.getCode() == 0) {
                            view.initSuccess(null);
                        } else {
                            view.initFail(gBean.getMsg());
                        }



                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });

    }
}
